import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';

class HomePage1 extends StatefulWidget {
  const HomePage1({super.key});

  @override
  State<HomePage1> createState() => _HomePageState1();
}

class _HomePageState1 extends State<HomePage1>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Random _random;
  late Timer _timer;
  List<String> images = [
    "pic/ccc1.png",
    "pic/bbb.png",
  ];
  int currentImageIndex = 0;

  @override
  void initState() {
    super.initState();
    _random = Random();
    _controller = AnimationController(
      vsync: this,
      lowerBound: 0.4,
      upperBound: 1,
      duration: const Duration(seconds: 3),
    )..repeat(reverse: true);
    _timer =
        Timer.periodic(const Duration(seconds: 2), (Timer t) => _changeImage());
    Future.delayed(const Duration(seconds: 3)).then((_) {
      Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (context) => const HomePage2()));
    });
  }

  void _changeImage() {
    setState(() {
      currentImageIndex = _random.nextInt(images.length);
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: AnimatedBuilder(
          animation: _controller,
          builder: (BuildContext context, Widget? child) {
            return Opacity(
              opacity: _controller.value,
              child: Column(children: [
                const Padding(padding: EdgeInsets.fromLTRB(0, 0, 0, 200)),
                SizedBox(
                    height: 150,
                    width: 150,
                    child: Image.asset(
                      images[currentImageIndex],
                      fit: BoxFit.fill,
                    )),
                const Padding(padding: EdgeInsets.fromLTRB(0, 0, 0, 50)),
                const Text(
                  "识     海",
                  style:
                      TextStyle(fontFamily: "YunFengFeiYunTi-2", fontSize: 50),
                ),
                const Text(
                  "属 于 你 的 虚 拟 社 交 圈",
                  style: TextStyle(fontSize: 23, color: Colors.black),
                ),
              ]),
            );
          },
        ),
      ),
    );
  }
}

class HomePage2 extends StatefulWidget {
  const HomePage2({super.key});

  @override
  State<HomePage2> createState() => _HomePageState2();
}

class _HomePageState2 extends State<HomePage2> {
  final PageController _pageController = PageController();
  bool _showButton = false;

  @override
  void initState() {
    super.initState();
    _pageController.addListener(() {
      if (_pageController.page == 3) {
        setState(() {
          _showButton = true;
        });
      } else {
        setState(() {
          _showButton = false;
        });
      }
    });
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          PageView(
            controller: _pageController,
            children: [
              Container(
                width: double.infinity,
                height: double.infinity,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("pic/aaa.png"),
                    repeat: ImageRepeat.repeatY,
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
              Container(
                width: double.infinity,
                height: double.infinity,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("pic/aaa2.png"),
                    repeat: ImageRepeat.repeatY,
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
              Container(
                width: double.infinity,
                height: double.infinity,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("pic/aaa3.png"),
                    repeat: ImageRepeat.repeatY,
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
              Container(
                width: double.infinity,
                height: double.infinity,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("pic/aaa4.png"),
                    repeat: ImageRepeat.repeatY,
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
            ],
          ),
          Center(
            child: AnimatedOpacity(
              opacity: _showButton ? 1.0 : 0.0,
              duration: const Duration(seconds: 1),
              child: Container(
                child: Visibility(
                  visible: _showButton,
                  child: ElevatedButton(
                    onPressed: () {
                      // Button action goes here
                      Navigator.pushNamed(context, '/tab');
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.white,
                      padding:
                          EdgeInsets.symmetric(horizontal: 50, vertical: 15),
                    ),
                    child: const Text("开始旅行",style: TextStyle(color: Colors.black,fontFamily: 'DingTalk',fontSize: 20.0),),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
